package com.shu.logsearch.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author Tree Cai
 * @date 2019/7/4
 */
public class MyIOStreamUtil {

    private static Logger logger = LoggerFactory.getLogger(MyIOStreamUtil.class);
    /**
     * 默认缓冲池大小
     */
    private static final Integer DEFAULT_BUFFER_SIZE = 1024;

    /**
     * 将inputStream输出到outputStream
     *
     * @param inputStream
     * @param outputStream
     * @param close        是否关闭流
     */
    public static void transferTo(InputStream inputStream, OutputStream outputStream, boolean close) throws IOException {
        try {
            int num;
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            while ((num = inputStream.read(buffer, 0, buffer.length)) > 0)
                outputStream.write(buffer, 0, num);
        } finally {
            if (close) {
                CloseUtil.close(inputStream);
                CloseUtil.close(outputStream);
            }
        }

    }

}
